package com.zxl.live.screen.screen.ui.helper;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;

import com.zxl.live.screen.R;
import com.zxl.live.screen.screen.constant.ScreenSetting;
import com.zxl.live.screen.ui.activity.MainActivity;

/**
 * Created by Administrator on 2017/11/10.
 */

public class NotifHelper {

    // 添加常驻通知
    public static void showNotification(Context context) {
        Notification.Builder builder = new Notification.Builder(context);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setTicker(context.getResources().getString(R.string.app_name));
        builder.setContentTitle(context.getResources().getStringArray(R.array.efficet_type)[ScreenSetting.sEffciencyType]);
        builder.setContentText(context.getString(R.string.notification_information));

        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        intent.setClass(context, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        builder.setContentIntent(PendingIntent.getActivity(context, 0, intent, 0));

        Notification notification = builder.getNotification();
        notification.flags = Notification.FLAG_ONGOING_EVENT; // 设置常驻 Flag

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(R.string.app_name, notification);
    }

    public static void stopNotification(Context context) {
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.cancel(R.string.app_name);
    }


    public static Notification getNotification(Context context) {
        Notification.Builder builder = new Notification.Builder(context);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setTicker(context.getResources().getString(R.string.app_name));
        builder.setContentTitle(context.getResources().getStringArray(R.array.efficet_type)[ScreenSetting.sEffciencyType]);
        builder.setContentText(context.getString(R.string.notification_information));

        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        intent.setClass(context, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        builder.setContentIntent(PendingIntent.getActivity(context, 0, intent, 0));
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            return builder.build();
        } else {
            return builder.getNotification();
        }
    }


}
